
import importlib
from scipy import integrate

import pat.utils as paut


def load(type, variant, param=None):
    mod_str = "pat.vehicles.{:s}.dynamic_model_{:s}".format(type, variant)
    dm = importlib.import_module(mod_str)
    return dm.DynamicModel(param)


class DynamicModel():

    def __init__(self, param=None):
        self.P = None
        self.X = None
        self.Xd = None
    
    def reset(self, X0=None, U0=None):
        self.X = X0
        if U0<>None: self.Xd = self.dyn(self.X, 0, U0, self.P)
        print "dm:reset", self.X
        return self.X

    def run(self, dt, U):
        foo, self.X = integrate.odeint(self.dyn, self.X, [0, dt], args=(U, self.P, ))
        self.Xd = self.dyn(self.X, 0, U, self.P)
        return self.X

    def trim(self, args=None):
        return None, None

    def jacobian(self, Xe, Ue):
        return paut.num_jacobian(Xe, Ue, self.P, self.dyn)

    def name(self):
        return "None"

    def state_str(self):
        return "None"

    def param(self):
        return "None"
